window.addEventListener('load',function(){
   $(function(){
    var arrow_l = document.querySelector('.arrow-l');
    var arrow_r = document.querySelector('.arrow-r');
    var focus = document.querySelector('.focus');
    var focusWidth = focus.offsetWidth;
    focus.addEventListener('mouseenter',function(){
        arrow_l.style.display = 'block';
        arrow_r.style.display = 'block';
        clearInterval(timerr);
        timerr = null;
    });
    focus.addEventListener('mouseleave',function(){
        arrow_l.style.display = 'none';
        arrow_r.style.display = 'none';
        timerr = setInterval(function(){
            arrow_r.click();
        },2000)
    });

    //动态生成小圆圈 有几个图 就生成几个小圆圈
    var ul = focus.querySelector('ul');
    var ol = focus.querySelector('.circle');

    //console.log(ul.children.length);
    for (var i = 0; i < ul.children.length; i++){
        var li = document.createElement('li');
        //记录小圆圈的索引号
        li.setAttribute('index',i);
        ol.appendChild(li);
        //排他思想 干掉所有人 留下我自己
        li.addEventListener('click',function() {
            for (var i = 0; i < ol.children.length; i++){
                ol.children[i].className = '';
            }
            this.className = 'current';
            //点击小圆圈，滚动图片移动的是ul
            //移动的距离是小圆圈的索引号x距离
            
            //拿到当前小li的索引号
            var index = this.getAttribute('index');
            num  = index;
            circle = index;
            //点击了小li拿到索引号
            console.log(focusWidth);
            console.log(index);
            animate(ul,-index * focusWidth);
        });
    }
    ol.children[0].className = 'current';
    //克隆第一张图片
    var first = ul.children[0].cloneNode(true);
    ul.appendChild(first);
    //点击右侧按钮 图片滚动一张
    var num = 0;
    //记录到那个小圆圈了
    var circle = 0;
    var flag = true;
    arrow_r.addEventListener('click', function(){
        if(flag) {
            flag = false;
            if(num == ul.children.length - 1){
                ul.style.left = 0;
                num = 0;
            }
            num++;
            animate(ul, -num * focusWidth,function(){
                flag = true;
            });
            //先清除其他小圆圈
            circle++;
            if(circle == ol.children.length) {
                circle = 0;
            }
            circleChange();
        }
        
    });

//点击左侧按钮 图片滚动一张
//记录到那个小圆圈了
arrow_l.addEventListener('click', function(){
    if(flag){
        flag = false;
        if(num == 0){
            num = ul.children.length - 1;
            ul.style.left = -num * focusWidth + 'px';
        }
        num--;
        animate(ul, -num * focusWidth,function(){
            flag = true;
        });
        circle--;
        // if(circle < 0) {
        //     circle = ol.children.length - 1;
        // }
        circle = circle < 0 ? ol.children.length - 1 : circle;
        //先清除其他小圆圈
        circleChange();
    }
})
function circleChange(){
    for(var i = 0; i < ol.children.length; i++) {
        ol.children[i].className = '';
    }
    //再赋值给当前的小圆圈
    ol.children[circle].className = 'current';
}
//自动播放
var timerr = setInterval(function(){
    arrow_r.click();
},2000);
   })
});